package com.mxs.activemq.usemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsConsumerAsyn {
    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;

    private static final int SENDNUM= 3;

    public static void main(String[] args){

        ConnectionFactory connectionFactory ;
        Connection connection = null;
        Session session;
        Destination destination ;
        MessageConsumer messageConsumer;
        try {
            connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            //destination = session.createTopic("HelloWorld");
            destination = session.createQueue("HelloWorld_queue");

            messageConsumer = session.createConsumer(destination);
            messageConsumer.setMessageListener(new MessageListener() {

                public void onMessage(Message message) {
                    try {
                        System.out.println(((TextMessage)message).getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });

        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
